#include "Server.h"
#include <As/Http/SocketHttpAdapter.h>
#include <As/Logic/ControlLogic.h>

#include <QSqlDatabase>

namespace As {

Server::Server(QObject *parent) :
    QObject(parent)
{
    m_settings = new QSettings("spbu", "authserver", this);
    m_httpAdapter = new SocketHttpAdapter(
		m_settings->value("Common/Host").toString(),
		m_settings->value("Common/Port").toInt(),
		this);
    m_controlLogic = new ControlLogic(this);

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("accounts");
    qDebug() << "opening database:" << db.open();

    qRegisterMetaType<As::HttpRequest>();
    qRegisterMetaType<As::HttpResponse>();
    connect(m_httpAdapter, SIGNAL(request(As::HttpRequest)),
            m_controlLogic, SLOT(request(As::HttpRequest)), Qt::QueuedConnection);
    connect(m_controlLogic, SIGNAL(response(As::HttpResponse)),
            m_httpAdapter, SLOT(response(As::HttpResponse)), Qt::QueuedConnection);
    connect(m_controlLogic, SIGNAL(listen()),
            m_httpAdapter, SLOT(listen()), Qt::QueuedConnection);
    connect(m_controlLogic, SIGNAL(stopListen()),
            m_httpAdapter, SLOT(stopListen()), Qt::QueuedConnection);
}

Server::~Server() {
    m_settings->sync();
}

}
